package com.example.hasee.zhcd.Activities.ChuanWu;

import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.util.Base64;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.example.hasee.zhcd.Base.BaseActivity;
import com.example.hasee.zhcd.Bean.EventMsg;
import com.example.hasee.zhcd.R;
import com.example.hasee.zhcd.Util.ProgressDialogUtils;

import org.json.JSONObject;

import java.util.HashMap;

/**
 * Created by hasee on 2018/5/31.
 */

public class NearDetailActivity extends BaseActivity {
    private TextView title;
    private ImageView back;
    private ImageView home;
    private String strSyn;

    private TextView fberth;
    private TextView fshipname;
    private TextView fhc;
    private TextView fwtf;
    private TextView fzygs;
    private TextView fdgsj;
    private TextView fkbsj;
    private TextView fkgsj;
    private TextView fwgsj;
    private TextView farrport;
    private TextView fht;
    private TextView fyb;
    private TextView fpurpose;
    private TextView fitem;
    private TextView fpzd;
    private TextView fjhd;
    private TextView fps;
    private TextView fsqys;
    private TextView fwxp;
    private String bmpString;
    private ImageView pictrue;

    //    private TextView fscandoc;
    @Override
    public int contentViewId() {
        return R.layout.activity_near_detail;
    }

    @Override
    public void set() {
        title.setText("靠泊详情");
        back.setOnClickListener(this);
        home.setVisibility(View.INVISIBLE);
        pictrue.setOnClickListener(this);
        Log.e("strSyn", strSyn);
        getKBDetail();
    }

    @Override
    public void init() {
        strSyn = getIntent().getExtras().getString("strSyn");
        back = (ImageView) findViewById(R.id.back);
        home = (ImageView) findViewById(R.id.home);
        title = (TextView) findViewById(R.id.centertitle);
        fberth = (TextView) findViewById(R.id.fberth);
        fshipname = (TextView) findViewById(R.id.fshipname);
        fhc = (TextView) findViewById(R.id.fhc);
        fwtf = (TextView) findViewById(R.id.fwtf);
        fzygs = (TextView) findViewById(R.id.fzygs);
        fdgsj = (TextView) findViewById(R.id.fdgsj);
        fkbsj = (TextView) findViewById(R.id.fkbsj);
        fkgsj = (TextView) findViewById(R.id.fkgsj);
        fwgsj = (TextView) findViewById(R.id.fwgsj);
        farrport = (TextView) findViewById(R.id.farrport);
        fht = (TextView) findViewById(R.id.fht);
        fyb = (TextView) findViewById(R.id.fyb);
        fpurpose = (TextView) findViewById(R.id.fpurpose);
        fitem = (TextView) findViewById(R.id.fitem);
        fpzd = (TextView) findViewById(R.id.fpzd);
        fjhd = (TextView) findViewById(R.id.fjhd);
        fps = (TextView) findViewById(R.id.fps);
        fsqys = (TextView) findViewById(R.id.fsqys);
        fwxp = (TextView) findViewById(R.id.fwxp);
        pictrue = (ImageView) findViewById(R.id.picture);
    }

    private void getKBDetail() {
        ProgressDialogUtils.createProgressDialog(NearDetailActivity.this, "正在获取数据...");
        HashMap<String, String> map = new HashMap<String, String>();
        map.put("strSyn", strSyn);
        httpUtils.get("ZHCW_kbcbDetail", map, "ZHCW_kbcbDetail");
    }

    @Override
    public void getEventData(EventMsg msg) {
        switch (msg.getAction()) {
            case "Fail":
                ProgressDialogUtils.closeProgressDialog();
                Toast.makeText(NearDetailActivity.this, "操作失败，请检查网络！", Toast.LENGTH_LONG).show();
                break;
            case "ZHCW_kbcbDetail":
                ProgressDialogUtils.closeProgressDialog();
//                Log.e("ZHCW_kbcbDetail", msg.getMsg());
                //ZHCW_kbcbDetail: {"data":{"fberth":"906", "fshipname":"银桦1", "fhc":"119",
                // "fwtf":"上海银桦航运有限公司", "fzygs":"九", "fdgsj":"05-27 15:35",
                // "fkbsj":"", "fkgsj":"", "fwgsj":"",
                // "farrport":"江阴港/江阴", "fht":"", "fyb":"",
                // "fpurpose":"装货", "fitem":"煤炭", "fpzd":"47400", "
                // fjhd":"46500", "fps":"10", "fsqys":"",
                // "fwxp":"已申报", "fscandoc":""},"rspcode": 0,"rspmessage":""}
                try {
                    JSONObject json = new JSONObject(msg.getMsg());
                    String rspcode = json.getString("rspcode");
                    String rspmessage = json.getString("rspmessage");
                    if ("0".equals(rspcode)) {
                        JSONObject data = json.getJSONObject("data");
                        fberth.setText(data.getString("fberth"));
                        fshipname.setText(data.getString("fshipname"));
                        fhc.setText(data.getString("fhc"));
                        fwtf.setText(data.getString("fwtf"));
                        fzygs.setText(data.getString("fzygs"));
                        fdgsj.setText(data.getString("fdgsj"));
                        fkbsj.setText(data.getString("fkbsj"));
                        fkgsj.setText(data.getString("fkgsj"));
                        fwgsj.setText(data.getString("fwgsj"));
                        farrport.setText(data.getString("farrport"));
                        fht.setText(data.getString("fht"));
                        fyb.setText(data.getString("fyb"));
                        fpurpose.setText(data.getString("fpurpose"));
                        fitem.setText(data.getString("fitem"));
                        fpzd.setText(data.getString("fpzd"));
                        fjhd.setText(data.getString("fjhd"));
                        fps.setText(data.getString("fps"));
                        fsqys.setText(data.getString("fsqys"));
                        fwxp.setText(data.getString("fwxp"));
                        bmpString = data.getString("fscandoc");
                        pictrue.setImageBitmap(base64ToBitmap(data.getString("fscandoc")));
                    } else {

                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
                break;
        }
    }

    public static Bitmap base64ToBitmap(String base64Data) {
        byte[] bytes = Base64.decode(base64Data, Base64.DEFAULT);
        return BitmapFactory.decodeByteArray(bytes, 0, bytes.length);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.back:
                finish();
                break;
            case R.id.picture:
                Intent intent = new Intent(NearDetailActivity.this, PictrueActivity.class);
                intent.putExtra("string", bmpString);
                startActivity(intent);
                break;
        }
    }
}
